package com.java.app.hacktouch.parameter

import android.content.SharedPreferences
import android.util.Log

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.java.app.hacktouch.mouse.MouseParams

/**
 * Created by guanguojin on 2017/11/6.
 */

class ParamsDB(private val sp: SharedPreferences) {
    private var mMouseParams: MouseParams? = null

    init {

        val gson = Gson()
        val mouseParamsStr = sp.getString(MOUSE_TAG, NULL_STR)
        if (mouseParamsStr == NULL_STR == true) {
            Log.d(TAG, "use default mouse params!!!")
            mMouseParams = MouseParams()
            mMouseParams!!.xOffset = 3
            mMouseParams!!.yOffset = 3
            mMouseParams!!.cursorStep = 7
            mMouseParams!!.circleSize = 20

            val editor = sp.edit()
            editor.putString(MOUSE_TAG, gson.toJson(mMouseParams))
            editor.commit()

        } else {
            mMouseParams = gson.fromJson<MouseParams>(mouseParamsStr, object : TypeToken<MouseParams>() {

            }.type)
        }
    }

    fun GetMouseParams(): MouseParams? {
        return mMouseParams
    }

    fun SetMouseParams(params: MouseParams) {
        val gson = Gson()

        //save
        val editor = sp.edit()
        editor.putString(MOUSE_TAG, gson.toJson(mMouseParams))
        editor.commit()
        //update
        mMouseParams = params
    }

    fun GetBTParams(): String {
        return sp.getString(BLUETOOTH_TAG, BT_DEFAULT_STR)
    }

    fun SetBTParams(params: String): String {
        val editor = sp.edit()
        editor.putString(BLUETOOTH_TAG, params)
        editor.commit()

        return GetBTParams()
    }

    companion object {

        private val TAG = "ParamsDB"
        private val NULL_STR = "null"
        private val BT_DEFAULT_STR = "hacktouch"

        val MOUSE_TAG = "MOUSE_PARAMS"
        val GESTURE_TAG = "GESTURE_PARAMS"
        val BLUETOOTH_TAG = "BLUETOOTH_PARAMS"
    }
}
